﻿Imports System.IO
Imports Model
Imports Helper
Imports Operation
Imports Model.Project
Imports Model.Midline
Imports Model.Geometry

Public Class NewProject

    Public tempmidline As midline
    Public tempendptx As Double = 0
    Public tempendpty As Double = 0

    Public newpro As New MyProject()
    Public ml As New midline()
    Public vline As bpd
    Public bpdlist As New List(Of bpd)()

    ''' <summary>
    ''' 窗体加载初始化
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub NewProject_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        bt_Pre.Enabled = False
        Tab02.Parent = Nothing
        Tab04.Parent = Nothing
        Tab03.Parent = Nothing
        Tab01.Select()
        Item_list.SelectedIndex = 0

        newpro = New MyProject()

        bpdlist.Clear()
    End Sub
    ''' <summary>
    ''' 选择目录
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub bt_SelDir_Click(sender As System.Object, e As System.EventArgs) Handles bt_SelDir.Click
        Dim FO As New FolderBrowserDialog()
        If FO.ShowDialog = Windows.Forms.DialogResult.OK Then
            txb_Dir.Text = FO.SelectedPath
        End If
    End Sub
    ''' <summary>
    ''' 上一步
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub bt_Pre_Click(sender As System.Object, e As System.EventArgs) Handles bt_Pre.Click

        Select Case TabCon.SelectedTab.Name
            Case "Tab02"
                Tab01.Parent = TabCon
                Tab01.Select()
                Tab02.Parent = Nothing
                Tab04.Parent = Nothing
                Tab03.Parent = Nothing
                bt_Pre.Enabled = False
                bt_Next.Enabled = True
                Item_list.SelectedIndex = 0
            Case "Tab03"
                Tab02.Parent = TabCon
                Tab02.Select()
                Tab01.Parent = Nothing
                Tab04.Parent = Nothing
                Tab03.Parent = Nothing
                bt_Pre.Enabled = True
                bt_Next.Enabled = True
                Item_list.SelectedIndex = 1
            Case "Tab04"
                Tab03.Parent = TabCon
                Tab03.Select()
                Tab01.Parent = Nothing
                Tab04.Parent = Nothing
                Tab02.Parent = Nothing
                bt_Pre.Enabled = True
                bt_Next.Enabled = True
                Item_list.SelectedIndex = 2
            Case Else

        End Select

    End Sub
    ''' <summary>
    ''' 下一步
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub bt_Next_Click(sender As System.Object, e As System.EventArgs) Handles bt_Next.Click
        Select Case TabCon.SelectedTab.Name
            Case "Tab02"
                Tab03.Parent = TabCon
                Tab03.Select()
                Tab01.Parent = Nothing
                Tab02.Parent = Nothing
                Tab04.Parent = Nothing
                bt_Pre.Enabled = True
                bt_Next.Enabled = True
                Item_list.SelectedIndex = 2
            Case "Tab01"
                Tab02.Parent = TabCon
                Tab02.Select()
                Tab01.Parent = Nothing
                Tab04.Parent = Nothing
                Tab03.Parent = Nothing
                bt_Pre.Enabled = True
                bt_Next.Enabled = True
                Item_list.SelectedIndex = 1
            Case "Tab03"
                Tab04.Parent = TabCon
                Tab04.Select()
                Tab01.Parent = Nothing
                Tab02.Parent = Nothing
                Tab03.Parent = Nothing
                bt_Pre.Enabled = True
                bt_Next.Enabled = False
                Item_list.SelectedIndex = 3
            Case Else

        End Select
    End Sub
    ''' <summary>
    ''' 完成新建工程,保存
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub bt_Finish_Click(sender As System.Object, e As System.EventArgs) Handles bt_Finish.Click

        If txb_Name.Text = "" Or txb_ID.Text = "" Or txb_Dir.Text = "" Then
            MsgBox("请将工程基本信息填写完整", , "提示")
            Return
        End If

        'If newpro.ProMidLine.Count <= 0 Then
        '    MsgBox("请输入隧道中线信息", , "提示")
        '    Return
        'End If

        '创建目录
        PATHHelper.createPro(newpro)
        Me.Cursor = Cursors.WaitCursor
        FILEHelper.createFile(newpro)
        XMLHelper.createXML(newpro)
        For i = 0 To newpro.ProPts.Count - 1
            newpro.ProPts(i) = newpro.ProDir + "\" + newpro.ProID + newpro.ProName + "\pointcloud\" + Path.GetFileName(newpro.ProPts(i))
        Next
        Me.Cursor = Cursors.Arrow
        MsgBox("工程创建成功！", , "提示")
        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub
    ''' <summary>
    ''' 选择项变化
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Item_list_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Item_list.SelectedIndexChanged

        Select Case Item_list.SelectedIndex
            Case 0
                Tab01.Parent = TabCon
                Tab01.Select()
                Tab02.Parent = Nothing
                Tab03.Parent = Nothing
                Tab04.Parent = Nothing
                bt_Pre.Enabled = False
                bt_Next.Enabled = True
            Case 1
                Tab02.Parent = TabCon
                Tab02.Select()
                Tab01.Parent = Nothing
                Tab04.Parent = Nothing
                Tab03.Parent = Nothing
                bt_Pre.Enabled = True
                bt_Next.Enabled = True
            Case 2
                Tab03.Parent = TabCon
                Tab03.Select()
                Tab01.Parent = Nothing
                Tab02.Parent = Nothing
                Tab04.Parent = Nothing
                bt_Pre.Enabled = True
                bt_Next.Enabled = True
            Case 3
                Tab04.Parent = TabCon
                Tab04.Select()
                Tab01.Parent = Nothing
                Tab02.Parent = Nothing
                Tab03.Parent = Nothing
                bt_Pre.Enabled = True
                bt_Next.Enabled = False
            Case Else

        End Select
    End Sub

    ''' <summary>
    ''' 保存基本信息
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub bt_Save1_Click(sender As System.Object, e As System.EventArgs) Handles bt_Save1.Click
        If txb_ID.Text <> "" Then
            newpro.ProID = txb_ID.Text
        Else
            MsgBox("请输入工程编号", , "请示")
            Return
        End If
        If txb_Name.Text <> "" Then
            newpro.ProName = txb_Name.Text
        Else
            MsgBox("请输入工程名称", , "提示")
            Return
        End If
        If txb_Dir.Text <> "" Then
            newpro.ProDir = txb_Dir.Text
        Else
            MsgBox("请选择工程路径", , "提示")
            Return
        End If
        MsgBox("工程基本信息保存成功", , "提示")
    End Sub
    ''' <summary>
    ''' 清空基本信息
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub bt_Clear_Click(sender As System.Object, e As System.EventArgs) Handles bt_Clear.Click
        txb_ID.Clear()
        txb_Name.Clear()
        txb_Dir.Clear()
    End Sub
    ''' <summary>
    ''' 选择点云文件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ptFileSel_Click(sender As System.Object, e As System.EventArgs) Handles ptFileSel.Click
        currptfile.Text = DataAccess.importdata()
    End Sub
    ''' <summary>
    ''' 保存点云数据
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ptFileSave_Click(sender As System.Object, e As System.EventArgs) Handles ptFileSave.Click
        newpro.ProPts.Clear()
        If ptList.Items.Count = 0 Then
            Return
        Else
            For i As Integer = 0 To ptList.Items.Count - 1
                newpro.ProPts.Add(ptList.Items(i))
            Next
            MsgBox("点云数据保存成功！", , "提示")
        End If
    End Sub
    ''' <summary>
    ''' 添加点云
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ptFileAdd_Click(sender As System.Object, e As System.EventArgs) Handles ptFileAdd.Click
        If currptfile.Text = "" Then
            Return
        Else
            ptList.Items.Add(currptfile.Text)
        End If
    End Sub
    ''' <summary>
    ''' 添加中线信息到datagridview
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub midline_Add_Click(sender As System.Object, e As System.EventArgs) Handles midline_Add.Click
        Dim temppt1, temppt2, temppt3 As New Point3d()
        Select Case c_LineType.Tag.ToString()
            Case "0"    '直线
                ml.id = dgv_linelist.RowCount + 1
                ml.LineType = LineTypeList.TypeLINE
                temppt1.p_x = Double.Parse(c_startpt.Text.Split(",")(0))
                temppt1.p_y = Double.Parse(c_startpt.Text.Split(",")(1))
                ml.startpt = temppt1
                ml.startm = Double.Parse(c_startm.Text)
                temppt2.p_x = Double.Parse(c_endpt.Text.Split(",")(0))
                temppt2.p_y = Double.Parse(c_endpt.Text.Split(",")(1))
                ml.endpt = temppt2
                ml.endm = Double.Parse(c_endm.Text)
                ml.length = 0
                ml.isturnright = True
                ml.midpt = New Point3d()
                ml.midm = 0
                ml.radius = 0
                ml.startRadius = 0
                ml.endRadius = 0
                ml.startoffset = 0
                ml.endoffset = 0
            Case "1"    '圆曲线
                ml.id = dgv_linelist.RowCount + 1
                ml.LineType = LineTypeList.TypeCIRCLE
                temppt1.p_x = Double.Parse(c_startpt.Text.Split(",")(0))
                temppt1.p_y = Double.Parse(c_startpt.Text.Split(",")(1))
                ml.startpt = temppt1
                ml.startm = Double.Parse(c_startm.Text)
                temppt2.p_x = Double.Parse(c_endpt.Text.Split(",")(0))
                temppt2.p_y = Double.Parse(c_endpt.Text.Split(",")(1))
                ml.endpt = temppt2
                ml.endm = Double.Parse(c_endm.Text)
                ml.length = Double.Parse(c_length.Text)
                ml.isturnright = Boolean.Parse(c_isturnright.Tag.ToString())
                If (c_midpt.Text <> "") Then
                    ml.midpt.p_x = Double.Parse(c_midpt.Text.Split(",")(0))
                    ml.midpt.p_y = Double.Parse(c_midpt.Text.Split(",")(1))
                Else
                    ml.midpt = New Point3d()
                End If
                If (c_midm.Text <> "") Then
                    ml.midm = Double.Parse(c_midm.Text)
                Else
                    ml.midm = 0
                End If
                ml.radius = Double.Parse(c_Radius.Text)
                ml.startRadius = 0
                ml.endRadius = 0
                ml.startoffset = Double.Parse(txt_startoffset.Text)
                ml.endoffset = Double.Parse(txt_endoffset.Text)
            Case "2"    '缓和曲线
                ml.id = dgv_linelist.RowCount + 1
                ml.LineType = LineTypeList.TypeSUPERCIRCLE
                temppt1.p_x = Double.Parse(c_startpt.Text.Split(",")(0))
                temppt1.p_y = Double.Parse(c_startpt.Text.Split(",")(1))
                ml.startpt = temppt1
                ml.startm = Double.Parse(c_startm.Text)
                temppt2.p_x = Double.Parse(c_endpt.Text.Split(",")(0))
                temppt2.p_y = Double.Parse(c_endpt.Text.Split(",")(1))
                ml.endpt = temppt2
                ml.endm = Double.Parse(c_endm.Text)
                ml.length = Double.Parse(c_length.Text)
                ml.isturnright = Boolean.Parse(c_isturnright.Tag.ToString())
                ml.midpt = New Point3d()
                ml.midm = 0
                ml.radius = 0
                ml.startRadius = Double.Parse(c_startRadius.Text)
                ml.endRadius = Double.Parse(c_endRadius.Text)
                ml.startoffset = Double.Parse(txt_startoffset.Text)
                ml.endoffset = Double.Parse(txt_endoffset.Text)
        End Select
        Dim index = dgv_linelist.Rows.Add()
        If ml.LineType = LineTypeList.TypeLINE Then
            dgv_linelist.Rows(index).Cells("LineType").Value = "0"
        ElseIf ml.LineType = LineTypeList.TypeCIRCLE Then
            dgv_linelist.Rows(index).Cells("LineType").Value = "1"
        Else
            dgv_linelist.Rows(index).Cells("LineType").Value = "2"
        End If
        dgv_linelist.Rows(index).Cells("ID").Value = ml.id.ToString()
        dgv_linelist.Rows(index).Cells("startpt").Value = ml.startpt.p_x.ToString() + "," + ml.startpt.p_y.ToString()
        dgv_linelist.Rows(index).Cells("startm").Value = ml.startm.ToString()
        dgv_linelist.Rows(index).Cells("endpt").Value = ml.endpt.p_x.ToString() + "," + ml.endpt.p_y.ToString()
        dgv_linelist.Rows(index).Cells("endm").Value = ml.endm.ToString()
        dgv_linelist.Rows(index).Cells("length").Value = ml.length.ToString()
        dgv_linelist.Rows(index).Cells("isturnright").Value = ml.isturnright.ToString()
        dgv_linelist.Rows(index).Cells("midpt").Value = ml.midpt.p_x.ToString() + "," + ml.midpt.p_y.ToString()
        dgv_linelist.Rows(index).Cells("midm").Value = ml.midm.ToString()
        dgv_linelist.Rows(index).Cells("radius").Value = ml.radius.ToString()
        dgv_linelist.Rows(index).Cells("startRadius").Value = ml.startRadius.ToString()
        dgv_linelist.Rows(index).Cells("endRadius").Value = ml.endRadius.ToString()
        dgv_linelist.Rows(index).Cells("startoffset").Value = ml.startoffset.ToString()
        dgv_linelist.Rows(index).Cells("endoffset").Value = ml.endoffset.ToString()

        c_startpt.Text = c_endpt.Text
        c_startm.Text = c_endm.Text
        c_startRadius.Text = c_endRadius.Text
        c_endpt.Text = ""
        c_endm.Text = ""
        c_endRadius.Text = ""
        c_length.Text = ""
        c_Radius.Text = ""
        c_isturnright.Text = ""
        c_LineType.Text = ""
        txt_startoffset.Text = txt_endoffset.Text
        txt_endoffset.Text = ""
    End Sub
    ''' <summary>
    ''' 保存中线信息到对象
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub midline_Save_Click(sender As System.Object, e As System.EventArgs) Handles midline_Save.Click
        If dgv_linelist.RowCount > 0 Then
            newpro.ProMidLine.Clear()
            Dim i As Integer = 0
            Dim Num = dgv_linelist.RowCount
            For i = 0 To Num - 1 Step 1
                ml = New midline()
                ml.id = Integer.Parse(dgv_linelist.Rows(i).Cells("ID").Value.ToString())
                Select Case dgv_linelist.Rows(i).Cells("LineType").Value.ToString()
                    Case "0"
                        ml.LineType = LineTypeList.TypeLINE
                    Case "1"
                        ml.LineType = LineTypeList.TypeCIRCLE
                    Case "2"
                        ml.LineType = LineTypeList.TypeSUPERCIRCLE
                End Select

                ml.startpt.p_x = Double.Parse(dgv_linelist.Rows(i).Cells("startpt").Value.ToString().Split(",")(0))
                ml.startpt.p_y = Double.Parse(dgv_linelist.Rows(i).Cells("startpt").Value.ToString().Split(",")(1))
                ml.endpt.p_x = Double.Parse(dgv_linelist.Rows(i).Cells("endpt").Value.ToString().Split(",")(0))
                ml.endpt.p_y = Double.Parse(dgv_linelist.Rows(i).Cells("endpt").Value.ToString().Split(",")(1))
                ml.startm = Double.Parse(dgv_linelist.Rows(i).Cells("startm").Value.ToString())
                ml.endm = Double.Parse(dgv_linelist.Rows(i).Cells("endm").Value.ToString())
                ml.length = Double.Parse(dgv_linelist.Rows(i).Cells("length").Value.ToString())
                ml.isturnright = Boolean.Parse(dgv_linelist.Rows(i).Cells("isturnright").Value.ToString())
                ml.midm = Double.Parse(dgv_linelist.Rows(i).Cells("midm").Value.ToString())
                ml.midpt.p_x = Double.Parse(dgv_linelist.Rows(i).Cells("midpt").Value.ToString().Split(",")(0))
                ml.midpt.p_y = Double.Parse(dgv_linelist.Rows(i).Cells("midpt").Value.ToString().Split(",")(1))
                ml.radius = Double.Parse(dgv_linelist.Rows(i).Cells("radius").Value.ToString())
                ml.startRadius = Double.Parse(dgv_linelist.Rows(i).Cells("startRadius").Value.ToString())
                ml.endRadius = Double.Parse(dgv_linelist.Rows(i).Cells("endRadius").Value.ToString())
                ml.startoffset = Double.Parse(dgv_linelist.Rows(i).Cells("startoffset").Value.ToString())
                ml.endoffset = Double.Parse(dgv_linelist.Rows(i).Cells("endoffset").Value.ToString())

                newpro.ProMidLine.Add(ml)
            Next (i)
            MsgBox("平曲线数据保存成功", , "提示")
        Else
            newpro.ProMidLine.Clear()
            Return
        End If
    End Sub
    ''' <summary>
    ''' 点击列表项中某一项，计算点云的相关参数信息
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ptList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ptList.SelectedIndexChanged
        Dim line(3) As String
        Dim temppt As New Point3d()
        Dim xmax As Double = -1000000
        Dim xmin As Double = 1000000
        Dim ymax As Double = -1000000
        Dim ymin As Double = 1000000
        Dim zmax As Double = -1000000
        Dim zmin As Double = 1000000
        Dim n = 0
        Dim sr As StreamReader = New StreamReader(ptList.SelectedItem.ToString(), System.Text.Encoding.UTF8)
        Do While sr.Peek() > 0
            line = sr.ReadLine().Split(",") '英文逗号分割每行数据
            If line(0) = "" Then
                Continue Do
            End If
            temppt.p_x = Double.Parse(line(0))
            temppt.p_y = Double.Parse(line(1))
            temppt.p_z = Double.Parse(line(2))
            If temppt.p_x > xmax Then
                xmax = temppt.p_x
            End If
            If temppt.p_x < xmin Then
                xmin = temppt.p_x
            End If
            If temppt.p_y > ymax Then
                ymax = temppt.p_y
            End If
            If temppt.p_y < ymin Then
                ymin = temppt.p_y
            End If
            If temppt.p_z > zmax Then
                zmax = temppt.p_z
            End If
            If temppt.p_z < zmin Then
                zmin = temppt.p_z
            End If
            n = n + 1
        Loop
        sr.Close()
        sr = Nothing

        Total.Text = n.ToString()
        Me.Xmax.Text = Math.Round(xmax, 3).ToString()
        Me.Xmin.Text = Math.Round(xmin, 3).ToString()
        Me.Ymax.Text = Math.Round(ymax, 3).ToString()
        Me.Ymin.Text = Math.Round(xmin, 3).ToString()
        Me.Zmax.Text = Math.Round(zmax, 3).ToString()
        Me.Zmin.Text = Math.Round(zmin, 3).ToString()
    End Sub

    Private Sub c_LineType_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles c_LineType.SelectedIndexChanged
        Select Case c_LineType.Text
            Case "直线"
                c_LineType.Tag = "0"
                c_midpt.Enabled = False
                c_length.Enabled = False
                c_midm.Enabled = False
                c_isturnright.Enabled = False
                c_Radius.Enabled = False
                c_startRadius.Text = "0"
                c_startRadius.Enabled = False
                c_endRadius.Text = "0"
                c_endRadius.Enabled = False
            Case "圆曲线"
                c_LineType.Tag = "1"
                c_midpt.Enabled = True
                c_length.Enabled = True
                c_midm.Enabled = True
                c_isturnright.Enabled = True
                c_isturnright.Text = ""
                c_Radius.Enabled = True
                c_startRadius.Text = ""
                c_startRadius.Enabled = False
                c_endRadius.Text = ""
                c_endRadius.Enabled = False
            Case "缓和曲线"
                c_LineType.Tag = "2"
                c_midpt.Enabled = True
                c_length.Enabled = True
                c_midm.Enabled = True
                c_isturnright.Enabled = True
                c_isturnright.Text = ""
                c_Radius.Enabled = True
                'c_startRadius.Text = ""
                c_startRadius.Enabled = True
                c_endRadius.Text = ""
                c_endRadius.Enabled = True
                'txt_endoffset.Text = ""
        End Select
    End Sub

    Private Sub c_isturnright_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles c_isturnright.SelectedIndexChanged
        If c_isturnright.SelectedItem = "左转" Then
            c_isturnright.Tag = "False"
        Else
            c_isturnright.Tag = "True"
        End If
    End Sub

    Private Sub bt_AddV_Click(sender As System.Object, e As System.EventArgs) Handles bt_AddV.Click
        vline = New bpd()
        vline.R = Double.Parse(VR.Text.ToString())
        vline.i1 = Double.Parse(Vi1.Text.ToString())
        vline.i2 = Double.Parse(Vi2.Text.ToString())
        vline.bpdm = Double.Parse(Vbpdm.Text.ToString())
        vline.elejd = Double.Parse(Velejd.Text.ToString())
        Dim index = dgv_vline.Rows.Add()
        dgv_vline.Rows(index).Cells("V_R").Value = vline.R.ToString()
        dgv_vline.Rows(index).Cells("i1").Value = vline.i1.ToString()
        dgv_vline.Rows(index).Cells("i2").Value = vline.i2.ToString()
        dgv_vline.Rows(index).Cells("bpdm").Value = vline.bpdm.ToString()
        dgv_vline.Rows(index).Cells("elejd").Value = vline.elejd.ToString()
        bpdlist.Add(vline)

        VR.Text = ""
        Vi1.Text = Vi2.Text
        Vi2.Text = ""
        Vbpdm.Text = ""
        Velejd.Text = ""
    End Sub

    Private Sub bt_SaveV_Click(sender As System.Object, e As System.EventArgs) Handles bt_SaveV.Click
        If Vstartm.Text = "" Then
            MsgBox("请输入起始里程", , "提示")
            Return
        End If
        If bpdlist.Count = 0 Then
            MsgBox("请添加至少一个变坡点信息", , "提示")
            Return
        End If
        newpro.VLine.startm = Double.Parse(Vstartm.Text.ToString())
        newpro.VLine.bpdlist = bpdlist
        MsgBox("竖曲线信息保存成功", , "提示")
    End Sub

    Private Sub Vstartm_TextChanged(sender As Object, e As EventArgs) Handles Vstartm.TextChanged

    End Sub

End Class